package com.yauchu.toolbar;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yauchu.kegra.graph.GraphNode;
import com.yauchu.kegra.graph.GraphNode;

public class OperatorsToolbar {

	private ArrayList<GraphNode> operators= new ArrayList<GraphNode>() ;
	private LinearLayout rootView=null;
	
	public OperatorsToolbar()
	{
		
	}
	
	public OperatorsToolbar(ArrayList<GraphNode> operators)
	{
		this.operators=operators;
	}
	
	public LinearLayout generateView(Context context)
	{
		// TODO think what may happen if context is changed
		if (rootView==null)  {
			rootView=new LinearLayout(context);
			rootView.setOrientation(LinearLayout.HORIZONTAL);
		}
		Iterator<GraphNode> opiter= operators.iterator();
		GraphNode currentGraphNode;
		ImageView currentImageView;
		while(opiter.hasNext())
		{
			//TODO rewrite normally probably I should make method that generate view for node
			
			currentGraphNode=opiter.next();
			currentImageView=currentGraphNode.imageView;
			
			rootView.addView(currentGraphNode.displayLayout);
			
			currentImageView.setOnLongClickListener(new ToolBarOnLongClickListener(currentGraphNode));
		}
		
		
		return rootView;
	}
	
	public void addToPanel(GraphNode newoperator)
	
	{
		operators.add(newoperator);
		this.refresh();
		
	}
	
	public void refresh()
	{
		generateView(rootView.getContext());
		rootView.invalidate();
	}
	
	
}
